import java.io.IOException;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;

public class EslPlayer extends HLPlayer{
	private int age;
	private int eslID;
	private boolean premium;
	private String memberSince;
	private String name;
	private enum TrustLevel{T0, T1, T2, T3, T4}

	private TrustLevel trustLevel;
	private String url;
	private final static String ESLURL = "http://www.esl.eu";
	private final static String SUBURLREGEX = "/de/cs/player/[0-9]+/";
	private final static Pattern SUBURLPATTERN = Pattern.compile(SUBURLREGEX);
//	private final static String AGEREGEX = "19 Years";
//	private final static Pattern AGEPATTERN = Pattern.compile(AGEREGEX);
	private final static String MEMBERSINCEREGEX = "\\d\\d/\\d\\d/\\d\\d";
	private final static Pattern MEMBESINCEPATTERN = Pattern.compile(MEMBERSINCEREGEX);
	private final static String NAMEREGEX = "ESL: ";
	private final static String PREMIUMREGEX = "_premiummember.gif";
	private final static String TRUSTREGEX = "_trustlevel_";
	private final static String ERROR_TRUSTEDNOTFOUND = "Trusted not found";
	
	public EslPlayer(String steamID) throws IOException, EslAccountNotFoundException{
		super(steamID);
		retriveUserData(steamID);
	}

	public void retriveUserData(String steamID) throws IOException, EslAccountNotFoundException{
		String searchSource;
		String profileSource;
		String usrSubUrl;
		HttpClient client = new HttpClient();
		client.getHostConfiguration().setHost("www.esl.eu", 80, "http");
		GetMethod getIDSearch = new GetMethod("/de/cs/search/?query=" + steamID);
		client.executeMethod(getIDSearch);
		searchSource = getIDSearch.getResponseBodyAsString();
		Matcher matcher = EslPlayer.SUBURLPATTERN.matcher(searchSource);
		if(!matcher.find())
			throw new EslAccountNotFoundException();
		usrSubUrl = matcher.group();
		this.eslID = Integer.parseInt(usrSubUrl.substring(14, usrSubUrl.length()-2));
		this.url = ESLURL + usrSubUrl;
		GetMethod getProfile = new GetMethod(usrSubUrl);
		client.executeMethod(getProfile);
		profileSource = getProfile.getResponseBodyAsString();
		//TODO Geknaubt NAME
		this.name = profileSource.substring(profileSource.indexOf(NAMEREGEX) + NAMEREGEX.length(), profileSource.indexOf(" -"));
		//Premium
		this.premium = profileSource.contains(PREMIUMREGEX);
		//Member since
		matcher = EslPlayer.MEMBESINCEPATTERN.matcher(profileSource);
		matcher.find();
		matcher.find(); //TODO eins wegmache
		memberSince = matcher.group();
		//Trusted
		int beg = profileSource.indexOf(TRUSTREGEX)+ TRUSTREGEX.length();
		int end = beg + 1;
		char trustLevelChar = profileSource.substring(beg, end).charAt(0);
		switch (trustLevelChar) {
		case '0':
			trustLevel = TrustLevel.T0;
			break;
		case '1':
			trustLevel = TrustLevel.T1;
			break;
		case '2':
			trustLevel = TrustLevel.T2;
			break;
		case '3':
			trustLevel = TrustLevel.T3;
			break;
		case '4':
			trustLevel = TrustLevel.T4;
			break;
		default:
			throw new PlayerException();
		}
	}
	
	public String getSteamID() {
		return steamID;
	}

	public int getEslID() {
		return eslID;
	}

	public String getUrl() {
		return url;
	}
	
	@Override
	public String toString(){
		StringBuffer sb = new StringBuffer("<html><body><p style=\"background-color : gray; font-family : Arial, sans-serif; font-size : 11px; font-style : normal;\"><a href=\"");
		sb.append(url).append("\">").append(name).append("</a>").append("<br>").append(steamID).append("<br>")
		.append(memberSince).append("</p></body></html>");
		return sb.toString();
	}
	
}
